import { createRouter, createWebHashHistory } from 'vue-router'
import store from '../store';

const routes = [
    {
        // 重定向到index
        path: '/',
        redirect: '/mall/index'
    },
    {
        // （管理员）管理界面主路由
        path: '/admin/',
        component: () => import('../views/Admin/AdminMain.vue'),
        redirect: '/admin/home',
        // 需要管理员权限
        meta: { requireAdmin: true, },
        children: [
            {
                // 首页
                path: 'home',
                component: () => import('../views/Admin/AdminHome.vue')
            },
            {
                // 商城利润账户
                path: 'webshop-account',
                component: () => import('../views/Admin/AdminWebShopAccount.vue')
            },
            {
                // 商品类别管理
                path: 'category',
                component: () => import('../views/Admin/Category/Category.vue')
            },
            {
                // 用户管理
                path: 'user',
                component: () => import('../views/Admin/AdminUser.vue')
            },
            {
                // 店铺管理
                path: 'store',
                component: () => import('../views/Admin/Store/Store.vue')
            },
            {
                // 商品管理
                path: 'commodity',
                component: () => import('../views/Admin/Commodity/Commodity.vue')
            },
            {
                // 开店申请管理
                path: 'store/open-request',
                component: () => import('../views/Admin/Store/OpenRequest.vue')
            },
            {
                // 关店申请管理
                path: 'store/delete-request',
                component: () => import('../views/Admin/Store/DeleteRequest.vue')
            },
            {
                // 商品上架申请管理
                path: 'commodity/launch-request',
                component: () => import('../views/Admin/Commodity/LaunchRequest.vue')
            },
            {
                // 商品修改申请管理
                path: 'commodity/modify-request',
                component: () => import('../views/Admin/Commodity/ModifyRequest.vue')
            },
            {
                // 活动列表
                path: "/admin/activity",
                component: () => import('../views/Admin/Activity/Activity.vue')
            },
            {
                // 参与活动的申请
                path: "/admin/activity/participate-request",
                component: () => import('../views/Admin/Activity/ParticipateInRequest.vue')
            },
        ]
    },
    {
        // （店主）商店管理界面
        path: '/store-manage/',
        component: () => import('../views/StoreManage/StoreManageMain.vue'),
        redirect: '/store-manage/home',
        // 需要登录和商户身份
        meta: { requireAuth: true, requireSeller: true, },
        children: [
            {
                // 首页
                path: 'home',
                component: () => import('../views/StoreManage/StoreManageHome.vue'),
            },
            {
                // 这家商店的账户
                path: 'account',
                component: () => import('../views/StoreManage/Account.vue')
            },
            {
                // 发货信息
                path: 'send-info',
                component: () => import('../views/StoreManage/SendInfo.vue'),
            },
            {
                // 退货退款申请
                path: 'revoke-request',
                component: () => import('../views/StoreManage/RevokeRequest.vue'),
            },
            {
                // 商品申请上架记录
                path: 'launch-request',
                component: () => import('../views/StoreManage/Commodity/LaunchRequest.vue'),
            },
            {
                // 商品修改申请记录
                path: 'modify-request',
                component: () => import('../views/StoreManage/Commodity/ModifyRequest.vue'),
            },
            {
                // 申请上架商品
                path: 'launch-commodity',
                component: () => import('../views/StoreManage/Commodity/LaunchCommodity.vue'),
            },
            {
                // 这家商店的商品
                path: 'commodity',
                component: () => import('../views/StoreManage/Commodity/Commodity.vue'),
            },
            {
                // 所有正在进行中的活动
                path: 'activity',
                component: () => import('../views/StoreManage/Activity/Ongoing.vue')
            },
            {
                // 申请参加活动的历史记录
                path: 'activity-request',
                component: () => import('../views/StoreManage/Activity/ParticipationRequest.vue')
            }
        ]
    },
    {
        // 登录界面
        path: '/login',
        component: () => import('../views/LoginAndRegister/LoginAndRegister.vue'),
    },
    {
        // 商城界面
        path: '/mall/',
        component: () => import('../views/Mall/MallMain.vue'),
        redirect: '/mall/index',
        children: [
            {
                // 首页
                path: 'index',
                component: () => import('../views/Mall/MallIndex.vue'),
            },
            {
                // 购物车
                path: 'cart',
                component: () => import('../views/Mall/MallCart.vue'),
                // 需要登录后才能进入
                meta: { requireAuth: true },
            },
            {
                // 商品页面
                path: 'commodity',
                component: () => import('../views/Mall/MallCommodity.vue'),
            },
            {
                // 商店页面
                path: 'store',
                component: () => import('../views/Mall/MallStore.vue'),
            },
            {
                // 跨店满减活动页面
                path: 'activity',
                component: () => import('../views/Mall/MallActivity.vue')
            },
            {
                // 订单预览页面
                path: 'order/preview',
                component: () => import('../views/Mall/Order/Preview.vue'),
                meta: { requireAuth: true },
            },
            {
                // 订单结算页面
                path: 'order/pay',
                component: () => import('../views/Mall/Order/Pay.vue'),
                meta: { requireAuth: true },
            },
            {
                // 用户中心
                path: 'user_center/',
                component: () => import('../views/Mall/MallUserCenter/Main.vue'),
                redirect: '/mall/user_center/userinfo',
                // 需要登录后才能进入
                meta: { requireAuth: true, },
                children: [
                    {
                        // 用户基本信息
                        path: 'userinfo',
                        component: () => import('../views/Mall/MallUserCenter/UserInfo.vue')
                    },
                    {
                        // 收货地址管理
                        path: 'address',
                        component: () => import('../views/Mall/MallUserCenter/Address.vue')
                    },
                    {
                        // 退款退货申请记录
                        path: 'revoke',
                        component: () => import('../views/Mall/MallUserCenter/Revoke.vue')
                    },
                    {
                        // 所有订单
                        path: 'order/all',
                        component: () => import('../views/Mall/MallUserCenter/Order/AllOrder.vue')
                    },
                    {
                        //待支付订单
                        path: 'order/pay',
                        component: () => import('../views/Mall/MallUserCenter/Order/WaitForPay.vue')
                    },
                    {
                        //待发货订单
                        path: 'order/send',
                        component: () => import('../views/Mall/MallUserCenter/Order/WaitForSend.vue')
                    },
                    {
                        //待收货订单
                        path: 'order/receive',
                        component: () => import('../views/Mall/MallUserCenter/Order/WaitForReceive.vue')
                    },
                    {
                        //已完成订单
                        path: 'order/finish',
                        component: () => import('../views/Mall/MallUserCenter/Order/Finish.vue')
                    },
                    {
                        //已撤销订单
                        path: 'order/revoke',
                        component: () => import('../views/Mall/MallUserCenter/Order/Revoke.vue')
                    },
                    {
                        //已退款订单
                        path: 'order/refund',
                        component: () => import('../views/Mall/MallUserCenter/Order/Refund.vue')
                    },
                    {
                        // 申请开店
                        path: 'open_store',
                        component: () => import('../views/Mall/MallUserCenter/Store/OpenStore.vue'),
                        // 需要商户身份
                        meta: { requireSeller: true, },
                    },
                    {
                        // 开店申请记录
                        path: 'open_request',
                        component: () => import('../views/Mall/MallUserCenter/Store/OpenRequest.vue'),
                        // 需要商户身份
                        meta: { requireSeller: true, },
                    },
                    {
                        // 关店申请记录
                        path: 'delete_request',
                        component: () => import('../views/Mall/MallUserCenter/Store/DeleteRequest.vue'),
                        // 需要商户身份
                        meta: { requireSeller: true, },
                    },
                    {
                        // 商店列表
                        path: 'stores',
                        component: () => import('../views/Mall/MallUserCenter/Store/Stores.vue'),
                        // 需要商户身份
                        meta: { requireSeller: true, },
                    },
                    {
                        // 账户充值
                        path: 'account/recharge',
                        component: () => import('../views/Mall/MallUserCenter/Account/RechargeAccount.vue')
                    },
                    {
                        // 个人账户
                        path: 'account/personal',
                        component: () => import('../views/Mall/MallUserCenter/Account/PersonalAccount.vue'),
                    },
                    {
                        // 商店账户
                        path: 'account/store',
                        component: () => import('../views/Mall/MallUserCenter/Account/StoreAccount.vue'),
                        // 需要商户身份
                        meta: { requireSeller: true, },
                    }
                ]
            },
        ]
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

// 路由守卫
router.beforeEach((to, from) => {
    // 进入确认订单界面，需要保证：1.登录状态 2.从页面点击正常进入
    if (to.path === '/mall/order/preview') {
        if (store.state.order.toPreview !== true) {
            return false;
        }
    }
    // 离开订单界面的时候，把toPreview关掉
    if (from.path === '/mall/order/preview') {
        if (store.state.order.toPreview === true) {
            store.commit('order/reverseToPreview');
        }
    }

    // 进入支付订单界面，需要保证：1.登录状态 2.从页面点击正常进入
    if (to.path === '/mall/order/pay') {
        if (store.state.order.toPay !== true) {
            return false;
        }
    }
    // 离开支付订单界面的时候，把toPay关掉
    if (from.path === '/mall/order/pay') {
        if (store.state.order.toPay === true) {
            store.commit('order/reverseToPay');
        }
    }

    if (to.meta.requireAdmin) {  // 需要（登录）管理员权限
        if (store.state.user.role !== 0) {
            return false;  // 直接拒绝
        }
    } else if (to.meta.requireAuth) {  // 需要登录
        if (!store.state.user.token) {
            return { path: '/login' };
        }
        if (to.meta.requireSeller) {  // 需要商户身份
            if (store.state.user.role !== 2) {
                return false;  // 直接拒绝
            }
        }
    }
});

export default router